package net.cl.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * 系统工具类
 * 
 * @author CaoLei
 */
public class SystemUtil {

	/**
	 * 获取当前系统属性
	 */
	public static String getProperty(String property) {
		return System.getProperty(property);
	}

	@Deprecated
	public static Properties getSystemEnvironment() {
		Properties prop = System.getProperties();
		BufferedReader br = null;
		InputStreamReader is = null;
		Process p = null;
		try {
			String OS = prop.getProperty("os.name").toLowerCase();
			Runtime r = Runtime.getRuntime();
			if (OS.indexOf("windows 9") > -1)
				p = r.exec("command.com /c set");
			else if ((OS.indexOf("nt") > -1) || (OS.indexOf("windows 20") > -1) || (OS.indexOf("windows xp") > -1))
				p = r.exec("cmd.exe /c set");
			else
				p = r.exec("env");
			is = new InputStreamReader(p.getInputStream());
			br = new BufferedReader(is);
			String line;
			while ((line = br.readLine()) != null) {
				int i = line.indexOf("=");
				if (i > -1) {
					String key = line.substring(0, i);
					String value = line.substring(i + 1);
					prop.setProperty(key, value);
				}
			}
			p.waitFor();
			return prop;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			try {
				if (p != null)
					p.destroy();
				if (is != null)
					is.close();
				if (br != null)
					br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 当前系统的默认换行符号
	 */
	public static String getLineSeparator() {
		return getProperty("line.separator");
	}

	/**
	 * 当前系统的默认编码格式
	 */
	public static String getSystemEncoding() {
		return getProperty("file.encoding");
	}

	public static String getOsName() {
		return getProperty("os.name");
	}

	public static String getPathSeparator() {
		return getProperty("path.separator");
	}

	/**
	 * 操作系统体系结构(x86或x64等)
	 */
	public static String getOsArchitecture() {
		return getProperty("os.arch");
	}

}
